package im.youni.horus.core.module;

/**
 * 全局配置
 */
public final class GlobalConfig {

    // vars
    public String instanceId;                                           // 服务id，默认与name保持一致
    public String name = "monitor";                                     // 服务名
    private String host;
    private int port = 0;
    private String endPoint;

    private static GlobalConfig _instance = new GlobalConfig();

    private GlobalConfig() {
    }

    public static GlobalConfig getInstance() {
        return _instance;
    }

    public String getRegUrl() {
        return this.endPoint + "/v1/agent/service/register";
    }

    public String getDeRegUrl() {
        return this.endPoint + "/v1/agent/service/deregister/" + this.instanceId;
    }

    /**
     * 初始化必须配置
     * @param endPoint
     * @param host
     * @param port
     */
    public void initConfig(String endPoint, String host, int port) {
        this.endPoint = endPoint;
        this.host = host;
        this.port = port;
        this.instanceId = String.format("%s-%s-%s", name, host, port);
    }

    /**
     * 输出注册consul的信息json
     * @return
     */
    public String toRegInfo() {
        return "{" +
                "\"id\":\"" + instanceId + "\"," +
                "\"name\":\"" + name + "\"," +
                "\"address\":\"" + host + "\"," +
                "\"port\":" + port +
                "}";
    }

}
